{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #最小时间差"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #math #string #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #数学 #字符串 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: findMinDifference"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #最小时间差"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一个 24 小时制（小时:分钟 <strong>\"HH:MM\"</strong>）的时间列表，找出列表中任意两个时间的最小时间差并以分钟数表示。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>timePoints = [\"23:59\",\"00:00\"]\n",
    "<strong>输出：</strong>1\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>timePoints = [\"00:00\",\"23:59\",\"00:00\"]\n",
    "<strong>输出：</strong>0\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 &lt;= timePoints.length&nbsp;&lt;= 2 * 10<sup>4</sup></code></li>\n",
    "\t<li><code>timePoints[i]</code> 格式为 <strong>\"HH:MM\"</strong></li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><meta charset=\"UTF-8\" />注意：本题与主站 539&nbsp;题相同：&nbsp;<a href=\"https://leetcode-cn.com/problems/minimum-time-difference/\">https://leetcode-cn.com/problems/minimum-time-difference/</a></p>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [569nqc](https://leetcode.cn/problems/569nqc/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [569nqc](https://leetcode.cn/problems/569nqc/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"23:59\",\"00:00\"]', '[\"00:00\",\"23:59\",\"00:00\"]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def getMinutes(t: str) -> int:\n",
    "    return ((ord(t[0]) - ord('0')) * 10 + ord(t[1]) - ord('0')) * 60 + (ord(t[3]) - ord('0')) * 10 + ord(t[4]) - ord('0')\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        n = len(timePoints)\n",
    "        if n > 1440:\n",
    "            return 0\n",
    "        timePoints.sort()\n",
    "        ans = float('inf')\n",
    "        t0Minutes = getMinutes(timePoints[0])\n",
    "        preMinutes = t0Minutes\n",
    "        for i in range(1, n):\n",
    "            minutes = getMinutes(timePoints[i])\n",
    "            ans = min(ans, minutes - preMinutes)  # 相邻时间的时间差\n",
    "            preMinutes = minutes\n",
    "        ans = min(ans, t0Minutes + 1440 - preMinutes)  # 首尾时间的时间差\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        n = len(timePoints)\n",
    "        minutes = [0 for i in range(n)]\n",
    "        for i in range(n):\n",
    "            k = timePoints[i].split(':')\n",
    "            minutes[i]=int(k[0])*60+int(k[1])\n",
    "        minutes.sort()\n",
    "        ans = minutes[0]+24*60-minutes[-1]\n",
    "        for i in range(1, n):\n",
    "            ans = min(ans, minutes[i]-minutes[i-1])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        if len(timePoints)>24*60:\n",
    "            return 0\n",
    "        mins = sorted(int(t[:2])*60+int(t[3:]) for t in timePoints)\n",
    "        mins.append(mins[0]+24*60)\n",
    "        return min(b-a for a,b in pairwise(mins))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        res=24*60\n",
    "        timePoints=sorted([int(time[:2])*60+int(time[3:]) for time in timePoints])\n",
    "        timePoints.append(timePoints[0])\n",
    "        for x,y in pairwise(timePoints):\n",
    "            res=min(res,abs(x-y),1440-abs(x-y))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        def parsetime(t):\n",
    "            # 把时间解析为分钟数表示\n",
    "            return ((ord(t[0]) - ord(\"0\")) * 10 + (ord(t[1]) - ord(\"0\"))) * 60 + (ord(t[3]) - ord(\"0\")) * 10 + ord(t[4]) - ord(\"0\")\n",
    "\n",
    "\n",
    "        timePoints.sort() # 按字典序排\n",
    "        ans = float(\"inf\")\n",
    "        t0 = parsetime(timePoints[0])\n",
    "        preminute = t0\n",
    "        for i in range(1,len(timePoints)):\n",
    "            cur = parsetime(timePoints[i])\n",
    "            ans = min(ans,cur-preminute) # 相邻时间差\n",
    "            preminute = cur\n",
    "        ans = min(ans,t0 + 1440 - preminute)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        if len(timePoints)>24*60:\n",
    "            return 0\n",
    "        mins=sorted(int(t[:2])*60+int(t[3:]) for t in timePoints)\n",
    "        mins.append(mins[0]+24*60)\n",
    "        return min(mins[i]-mins[i-1] for i in range(1,len(mins)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        dic_list = [None for _ in range(1440)]\n",
    "        for timePoint in timePoints:\n",
    "            l_temp = timePoint.split(':')\n",
    "            h = int(l_temp[0])\n",
    "            m = int(l_temp[1])\n",
    "            if dic_list[h*60+m]:\n",
    "                return 0\n",
    "            else:\n",
    "                dic_list[h*60+m]=True\n",
    "        first_idx = 1440-1\n",
    "        final_idx = -1\n",
    "        previous_idx = -1\n",
    "        res = 1440\n",
    "        for idx in range(1440):\n",
    "            if dic_list[idx]:\n",
    "                first_idx = min(first_idx, idx)\n",
    "                final_idx = max(final_idx, idx)\n",
    "                if previous_idx==-1:\n",
    "                    previous_idx = idx\n",
    "                else:\n",
    "                    res = min(idx-previous_idx, res)\n",
    "                    previous_idx=idx\n",
    "        res = min(res, first_idx+1440-final_idx)\n",
    "        return (res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        if len(timePoints)>24*60:return 0\n",
    "        mins = sorted([int(t[:2])*60+int(t[3:]) for t in timePoints])\n",
    "        mins.append(mins[0]+24*60) #补位一个最早的时间和最晚的时间\n",
    "        return min([mins[i]-mins[i-1] for i in range(1,len(mins))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        def dif(a,b):\n",
    "            k=abs(a-b)\n",
    "            return k if k<=720 else 1440-k\n",
    "\n",
    "        def tran(x):\n",
    "            i,j=x.split(':')\n",
    "            return int(i)*60+int(j)\n",
    "        \n",
    "        for i,x in enumerate(timePoints):\n",
    "            timePoints[i]=tran(x)\n",
    "        \n",
    "        timePoints.sort()\n",
    "        n=len(timePoints)\n",
    "        ans=720\n",
    "        for i in range(n):\n",
    "            ans=min(ans,dif(timePoints[i],timePoints[i-1]))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def getMinutes(t: str) -> int:\n",
    "    return ((ord(t[0]) - ord('0')) * 10 + ord(t[1]) - ord('0')) * 60 + (ord(t[3]) - ord('0')) * 10 + ord(t[4]) - ord('0')\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        timePoints.sort()\n",
    "        ans = inf\n",
    "        t0Minutes = getMinutes(timePoints[0])\n",
    "        preMinutes = t0Minutes\n",
    "        for i in range(1, len(timePoints)):\n",
    "            minutes = getMinutes(timePoints[i])\n",
    "            ans = min(ans, minutes - preMinutes)  # 相邻时间的时间差\n",
    "            preMinutes = minutes\n",
    "        ans = min(ans, t0Minutes + 1440 - preMinutes)  # 首尾时间的时间差\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        \n",
    "        if len(timePoints)>24*16:\n",
    "            return 0\n",
    "        time_minutes = [int(t[:2])*60 + int(t[3:]) for t in timePoints]\n",
    "        mins = sorted(time_minutes)\n",
    "        mins.append(mins[0]+24*60)\n",
    "        return min(mins[i]-mins[i-1] for i in range(1,len(mins)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        for i in range(len(timePoints)):\n",
    "            timePoints[i] = 60 * int(timePoints[i][:2]) + int(timePoints[i][3:])\n",
    "        timePoints.sort()\n",
    "        # print(timePoints)\n",
    "        Min = 1440\n",
    "        for i in range(len(timePoints)):\n",
    "            if i == len(timePoints) - 1:\n",
    "                Min = min(Min, timePoints[0] - timePoints[i] + 1440)\n",
    "                # print('if', Min)\n",
    "            else:\n",
    "                # print()\n",
    "                Min = min(Min, timePoints[i+1] - timePoints[i])\n",
    "                # print('else', Min)\n",
    "            if Min == 0:\n",
    "                break\n",
    "        return Min"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        for i in range(len(timePoints)):\n",
    "            timePoints[i] = int(timePoints[i][0]+timePoints[i][1])*60+int(timePoints[i][3]+timePoints[i][4])\n",
    "        timePoints.sort()\n",
    "        ret = timePoints[0] + 1440 - timePoints[-1]\n",
    "        for i in range(1,len(timePoints)):\n",
    "            x = timePoints[i]-timePoints[i-1]\n",
    "            if(ret > x):\n",
    "                ret = x\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def getMinutes(t: str) -> int:\n",
    "    return ((ord(t[0]) - ord('0')) * 10 + ord(t[1]) - ord('0')) * 60 + (ord(t[3]) - ord('0')) * 10 + ord(t[4]) - ord('0')\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        n = len(timePoints)\n",
    "        if n > 1440:\n",
    "            return 0\n",
    "        timePoints.sort()\n",
    "        ans = float('inf')\n",
    "        t0Minites = getMinutes(timePoints[0])\n",
    "        preMinites = t0Minites\n",
    "        for i in range(1, n):\n",
    "            minites = getMinutes(timePoints[i])\n",
    "            ans = min(ans, minites - preMinites)\n",
    "            preMinites = minites\n",
    "        ans = min(ans, t0Minites + 1440 - preMinites)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        for i in range(len(timePoints)):\n",
    "            timePoints[i] = int(timePoints[i][0]+timePoints[i][1])*60+int(timePoints[i][3]+timePoints[i][4])\n",
    "        timePoints.sort()\n",
    "        ret = timePoints[0] + 1440 - timePoints[-1]\n",
    "        print(timePoints[0] + 1440 - timePoints[-1])\n",
    "        for i in range(1,len(timePoints)):\n",
    "            x = timePoints[i]-timePoints[i-1]\n",
    "            print(x)\n",
    "            if(ret > x):\n",
    "                ret = x\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        n = 24*60\n",
    "        if len(timePoints)>n:\n",
    "            return 0\n",
    "        m = sorted([int(t[:2])*60+int(t[3:]) for t in timePoints])\n",
    "        m.append(m[0]+n)\n",
    "        return min(m[i]-m[i-1] for i in range(1,len(m)))\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, tt: List[str]) -> int:\n",
    "        tt.sort()\n",
    "        cr=tt[0]\n",
    "        er=int(cr[:2])*60+int(cr[3:5])\n",
    "        pr=er\n",
    "        ans=inf\n",
    "        for x in tt[1:]:\n",
    "            c=int(x[:2])*60+int(x[3:5])\n",
    "            ans=min(ans,abs(c-pr),abs(1440-c+pr))\n",
    "            pr=c\n",
    "        ans=min(ans,1440-pr+er)    \n",
    "        return ans    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        minutes = []\n",
    "        ans = 720\n",
    "        if len(timePoints)>1440:\n",
    "            return 0\n",
    "        #将列表转化为分钟的形式\n",
    "        for i in timePoints:\n",
    "            j = i.split(':')\n",
    "            minute = int(j[0])*60+int(j[1])\n",
    "            minutes.append(minute)\n",
    "        \n",
    "        minutes.sort()\n",
    "        #因为会出现23:59到00:00这种情况，这种情况差值为1，所以在列表末尾加上最小值+1440\n",
    "        minutes.append(minutes[0]+1440)\n",
    "        #循环列表找出相连差值最小即可\n",
    "        print(minutes)\n",
    "        for k in range(1,len(minutes)):\n",
    "            time = minutes[k]-minutes[k-1]\n",
    "            if ans>time:\n",
    "                ans=time\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        for idx in range(len(timePoints)):\n",
    "            val = timePoints[idx].split(\":\")\n",
    "            val = list(map(int, val))\n",
    "            val = val[0] * 60 + val[1]\n",
    "            timePoints[idx] = val\n",
    "\n",
    "        timePoints.sort()\n",
    "        timePoints.append(timePoints[0] + 24 * 60)\n",
    "        res = 24 * 60\n",
    "        for idx in range(len(timePoints) - 1):\n",
    "            if timePoints[idx + 1] - timePoints[idx] < res:\n",
    "                res = timePoints[idx + 1] - timePoints[idx]\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        if len(set(timePoints)) != len(timePoints) or len(timePoints)<=1:\n",
    "            return 0\n",
    "\n",
    "        timePoints = sorted([int(i.split(\":\")[0])*60 + int(i.split(\":\")[1]) for i in timePoints])\n",
    "        timeDeltas = [timePoints[i+1] - timePoints[i] for i in range(len(timePoints)-1)]\n",
    "        timeDeltas.append(24*60 - timePoints[-1] + timePoints[0])\n",
    "        return min(timeDeltas)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def tellDiffer(time1:str,time2:str) -> int:\n",
    "        time1 = time1.split(':')\n",
    "        time2 = time2.split(':')\n",
    "        for i in range(2):\n",
    "            if time1[i][0]=='0' and time1[i][1]!='0':\n",
    "                time1[i] = time1[i].replace('0','')\n",
    "            if time2[i][0]=='0' and time2[i][1]!='0':\n",
    "                time2[i] = time2[i].replace('0','')\n",
    "        time1 = list(map(eval,time1))\n",
    "        time2 = list(map(eval,time2))\n",
    "        time_diff = min(abs((time1[0]*60+time1[1])-(time2[0]*60+time2[1])),abs((time1[0]*60+time1[1])-(time2[0]*60+time2[1])+24*60),abs((time1[0]*60+time1[1])-(time2[0]*60+time2[1])-24*60))\n",
    "\n",
    "        return time_diff\n",
    "\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        min_diff = Solution.tellDiffer(timePoints[0],timePoints[1])\n",
    "        length = len(timePoints)\n",
    "        for i in range(length):\n",
    "            for t2 in timePoints[i+1:]:\n",
    "                diff = Solution.tellDiffer(timePoints[i],t2)\n",
    "                if(diff<min_diff):\n",
    "                    min_diff = diff\n",
    "                \n",
    "                if(min_diff==0):\n",
    "                    return 0\n",
    "        return min_diff\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def getMinutes(t: str) -> int:\n",
    "    return ((ord(t[0]) - ord('0')) * 10 + ord(t[1]) - ord('0')) * 60 + (ord(t[3]) - ord('0')) * 10 + ord(t[4]) - ord('0')\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        timePoints.sort()\n",
    "        ans = float('inf')\n",
    "        t0Minutes = getMinutes(timePoints[0])\n",
    "        preMinutes = t0Minutes\n",
    "        for i in range(1, len(timePoints)):\n",
    "            minutes = getMinutes(timePoints[i])\n",
    "            ans = min(ans, minutes - preMinutes)  # 相邻时间的时间差\n",
    "            preMinutes = minutes\n",
    "        ans = min(ans, t0Minutes + 1440 - preMinutes)  # 首尾时间的时间差\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        if True:\n",
    "            if len(timePoints)<=1: return 0\n",
    "            if len(timePoints) != len(set(timePoints)): return 0\n",
    "            timePoints.sort()\n",
    "            timePoints = [int(i.split(\":\")[0])*60 + int(i.split(\":\")[1]) for i in timePoints]\n",
    "            timeDeltas = [timePoints[i]-timePoints[i-1] for i in range(1, len(timePoints))]\n",
    "            timeDeltas.append( 24*60 - timePoints[-1] + timePoints[0])\n",
    "            return min(timeDeltas)\n",
    "            \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        else:\n",
    "\n",
    "            if len(set(timePoints)) != len(timePoints) or len(timePoints)<=1:\n",
    "                return 0\n",
    "\n",
    "            timePoints = sorted([int(i.split(\":\")[0])*60 + int(i.split(\":\")[1]) for i in timePoints])\n",
    "            timeDeltas = [timePoints[i+1] - timePoints[i] for i in range(len(timePoints)-1)]\n",
    "            timeDeltas.append(24*60 - timePoints[-1] + timePoints[0])\n",
    "            return min(timeDeltas)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        def qsort(left, right):\n",
    "            if left < right:\n",
    "                mid = partition(left, right)\n",
    "                qsort(left, mid-1)\n",
    "                qsort(mid+1, right)\n",
    "        \n",
    "        def partition(left, right):\n",
    "            p = timePoints[left]\n",
    "            i = left\n",
    "            for j in range(i+1, right+1):\n",
    "                if timePoints[j] < p:\n",
    "                    i += 1\n",
    "                    timePoints[i], timePoints[j] = timePoints[j], timePoints[i]\n",
    "            timePoints[i], timePoints[left] = timePoints[left], timePoints[i]\n",
    "            return i\n",
    "\n",
    "        def diff(time1, time2):\n",
    "            time2 = int(time2.split(':')[0]) * 60 + int(time2.split(':')[1])\n",
    "            time1= int(time1.split(':')[0]) * 60 + int(time1.split(':')[1])\n",
    "            d = time2 - time1\n",
    "            if d < 0:\n",
    "                d += 24 * 60\n",
    "            return d\n",
    "\n",
    "        qsort(0, len(timePoints)-1)\n",
    "        min_diff = inf\n",
    "        for i in range(1, len(timePoints)+1):\n",
    "            new_diff = diff(timePoints[i-1], timePoints[i % len(timePoints)])\n",
    "            if new_diff < min_diff:\n",
    "                min_diff = new_diff\n",
    "        return min_diff\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        # 最简单直接的办法就是给数组排序，这样时间点小的就在前面了,然后我们分别把小时和分钟提取出来，计算差值.\n",
    "        # 注意唯一的特殊情况就是第一个和末尾的时间点进行比较，第一个时间点需要加上24小时再做差值，\n",
    "        # https://www.cnblogs.com/grandyang/p/6568398.html\n",
    "        timePoints = sorted(timePoints)\n",
    "\n",
    "        res = float('inf')\n",
    "        n = len(timePoints)\n",
    "        diff = 0\n",
    "\n",
    "        for i in range(n):\n",
    "            t1, t2 = timePoints[i], timePoints[(i+1)%n]\n",
    "            diff_h = int(t2[:2]) - int(t1[:2])\n",
    "            diff_m = int(t2[3:5]) - int(t1[3:5])\n",
    "            diff = diff_h * 60 + diff_m\n",
    "            if i == n - 1:\n",
    "                diff += 24 * 60\n",
    "            \n",
    "            res = min(res, diff)\n",
    "        \n",
    "        return res\n",
    "\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from sortedcontainers import SortedList\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "      if(len(timePoints) > 24 * 60): return 0\n",
    "      n1 = int(''.join(timePoints[0].split(\":\")))\n",
    "      n1 = (n1 // 100) * 60 + n1 % 100\n",
    "      number = SortedList([n1])\n",
    "      result = float('inf')\n",
    "      for i in range(1, len(timePoints)):\n",
    "          temp = int(''.join(timePoints[i].split(\":\")))\n",
    "          temp = (temp // 100) * 60 + temp % 100\n",
    "          index = number.bisect_right(temp)\n",
    "          \n",
    "          if index==0 or index==len(number)-1:\n",
    "              cur_result = abs(temp - number[index])\n",
    "              result = min(result, cur_result)       \n",
    "\n",
    "          if -1<index-1:\n",
    "            cur_result=abs(temp - number[index - 1])\n",
    "            result = min(result, cur_result)\n",
    "          if index+1<len(number):\n",
    "            cur_result=abs(temp - number[index])\n",
    "            result = min(result, cur_result)\n",
    "\n",
    "          result = min(result, cur_result)\n",
    "          number.add(temp)\n",
    "\n",
    "      result = min(result, abs(1440 - number[-1] + number[0]))\n",
    "\n",
    "      return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        timePoints = sorted(timePoints)\n",
    "        res = 2400\n",
    "        # print(timePoints_new)\n",
    "        if timePoints[0] == timePoints[-1]:\n",
    "            return 0\n",
    "        else:\n",
    "            time_ear_h = int(timePoints[-1][:2])\n",
    "            time_ear_m = int(timePoints[-1][-2:])\n",
    "            # print(time_ear)\n",
    "            time_lat_h = int(timePoints[0][:2])\n",
    "            time_lat_m = int(timePoints[0][-2:])\n",
    "            flag = 0\n",
    "            if time_lat_m < time_ear_m:\n",
    "                flag = 1\n",
    "                time_lat_m +=60\n",
    "            time_int_m = time_lat_m - time_ear_m\n",
    "            time_lat_h += 24\n",
    "            time_int_h = time_lat_h-time_ear_h\n",
    "            if flag:\n",
    "                time_int_h-=1\n",
    "            time_int_all = time_int_h*100+time_int_m\n",
    "            if time_int_all < res:\n",
    "                res = time_int_all\n",
    "        for i in range(1,len(timePoints)):\n",
    "            if timePoints[i-1] == timePoints[i]:\n",
    "                return 0\n",
    "            time_ear_h = int(timePoints[i-1][:2])\n",
    "            time_ear_m = int(timePoints[i-1][-2:])\n",
    "            time_lat_h = int(timePoints[i][:2])\n",
    "            time_lat_m = int(timePoints[i][-2:])\n",
    "            flag = 0\n",
    "            if time_lat_m < time_ear_m:\n",
    "                flag = 1\n",
    "                time_lat_m +=60\n",
    "            time_int_m = time_lat_m - time_ear_m\n",
    "            # print(timePoints[i-1][:2])\n",
    "            # print(time_lat_h)\n",
    "            # print(time_ear_m)\n",
    "            # print(time_lat_m)\n",
    "            # print(time_int_m)\n",
    "            time_int_h = time_lat_h-time_ear_h\n",
    "            if flag:\n",
    "                time_int_h-=1\n",
    "            time_int_all = time_int_h*100+time_int_m\n",
    "            print(res)\n",
    "            if time_int_all < res:\n",
    "                res = time_int_all\n",
    "            # print(flag)\n",
    "            # print(time_int_h)\n",
    "            # print(res)\n",
    "            # print(time_int_m)\n",
    "            # print(time_int_all)\n",
    "        # process time_int\n",
    "        if res > 59:\n",
    "            time_int_m = res % 100\n",
    "            # print(time_int_m)\n",
    "            time_int_h = int(res/100)\n",
    "            # print(time_int_h)\n",
    "            res = int(time_int_h*60+time_int_m)\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        if len(set(timePoints)) != len(timePoints) or len(timePoints)<=1:\n",
    "            return 0        \n",
    "\n",
    "        timePoints = sorted([int(i.split(\":\")[0])*60 + int(i.split(\":\")[1]) for i in timePoints])\n",
    "        print(timePoints)\n",
    "        timeDeltas = [timePoints[i+1] - timePoints[i] for i in range(len(timePoints)-1)]\n",
    "        timeDeltas.append(24*60 - timePoints[-1] + timePoints[0])\n",
    "        return min(timeDeltas)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        save = list() \n",
    "        for i in range(len(timePoints)):\n",
    "            hour,minute = timePoints[i].split(':')\n",
    "            tmp = int(hour)*60 + int(minute)\n",
    "            save.append(tmp)\n",
    "            save.append(tmp+1440)\n",
    "        save.sort()  \n",
    "        res = float('inf')\n",
    "        for i in range(len(save)-1):\n",
    "            res = min(res,abs(save[i+1]-save[i]))\n",
    "            if res == 0:\n",
    "                return res   \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        nums = []\n",
    "        for t in timePoints:\n",
    "            h = t[0:2]\n",
    "            m = t[3:5]\n",
    "            x = int(h) * 60 + int(m)\n",
    "            nums.append(x)\n",
    "            nums.append(x + 24 * 60)\n",
    "\n",
    "        nums.sort()\n",
    "        print(nums)\n",
    "\n",
    "        n = len(nums)\n",
    "        res = []\n",
    "        for i in range(1, n):\n",
    "            x = nums[i - 1]\n",
    "            y = nums[i]\n",
    "            res.append(y - x)\n",
    "        return min(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        intlst, ret = [], float('inf')\n",
    "        for ch in timePoints:\n",
    "            tmplst = ch.split(':')\n",
    "            intlst.append(int(tmplst[0]) * 60 + int(tmplst[1]))\n",
    "        intlst.sort()\n",
    "        # 时间是一个环，需在列表尾加一个lst[0] + 24 * 60，实现首尾比较\n",
    "        intlst.append(intlst[0] + 1440)\n",
    "\n",
    "        for i in range(len(intlst) - 1):\n",
    "            ret = min(ret, intlst[i + 1] - intlst[i])\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        nums = []\n",
    "        for t in timePoints:\n",
    "            h = t[0:2]\n",
    "            m = t[3:]\n",
    "            x = int(h) * 60 + int(m)\n",
    "            nums.append(x)\n",
    "            nums.append(x + 24 * 60)\n",
    "\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        res = []\n",
    "        for i in range(1, n):\n",
    "            res.append(nums[i] - nums[i - 1])\n",
    "        return min(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        def cal_del(x, y):\n",
    "            delta = int(y[:2])*60+int(y[-2:])-(int(x[:2])*60+int(x[-2:]))\n",
    "            return min(delta, 1440-delta)\n",
    "\n",
    "        sorted_time = sorted(timePoints)\n",
    "        result = cal_del(sorted_time[0],sorted_time[-1])\n",
    "        \n",
    "        for x , y in zip(sorted_time[:-1],sorted_time[1:]):\n",
    "            result = min(result,cal_del(x,y))\n",
    "        # for i in range(len(sorted_time)-1):\n",
    "        #     for j in range(i+1, len(sorted_time)):\n",
    "        #         if sorted_time[i]==sorted_time[j]:\n",
    "        #             return 0\n",
    "        #         result = min(result,cal_del(sorted_time[i],sorted_time[j]))\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        def time2int(time: str):\n",
    "            hour, minute = time.split(':')\n",
    "            return int(hour) * 60 + int(minute)\n",
    "\n",
    "        minutes = []\n",
    "        for timePoint in timePoints:\n",
    "            minute = time2int(timePoint)\n",
    "            minutes.append(minute)\n",
    "            minutes.append(minute - 1440)\n",
    "\n",
    "        minutes = sorted(minutes)\n",
    "        min_diff = 1440\n",
    "        for i in range(len(minutes) - 1):\n",
    "            if minutes[i + 1] - minutes[i] < min_diff:\n",
    "                min_diff = minutes[i + 1] - minutes[i]\n",
    "\n",
    "        return min_diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        def time_str_to_int(s):\n",
    "            return int(s[0:2])*60 + int(s[3:5])\n",
    "        time_list = [time_str_to_int(x) for x in timePoints]\n",
    "        time_list.sort()\n",
    "        min_diff = abs(time_list[0] - time_list[1])\n",
    "        for i in range(len(time_list)):\n",
    "            a = time_list[i]\n",
    "            b = time_list[i+1] if i < len(time_list)-1 else time_list[0] + 24 * 60\n",
    "            min_diff = min(min_diff, abs(a - b))\n",
    "        return min_diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        n = len(timePoints)\n",
    "        timePointsNum = []\n",
    "        for i in range(n):\n",
    "            shi,fen = timePoints[i].split(':')\n",
    "            # print(shi)\n",
    "            # print(fen)\n",
    "            heapq.heappush(timePointsNum, int(shi)*60 + int(fen))\n",
    "        # print(timePointsNum)\n",
    "        startTime = heapq.heappop(timePointsNum)\n",
    "        preTime = startTime\n",
    "        res = 24*60 + 1\n",
    "        while timePointsNum:\n",
    "            tmp = heapq.heappop(timePointsNum)\n",
    "            if tmp == preTime:\n",
    "                return 0\n",
    "            else:\n",
    "                res = min(res,abs(tmp - preTime))\n",
    "                preTime = tmp\n",
    "        res = min(res,abs(24*60-preTime+startTime))\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        intlst, ret = [], float('inf')\n",
    "        for ch in timePoints:\n",
    "            tmplst = ch.split(':')\n",
    "            intlst.append(int(tmplst[0]) * 60 + int(tmplst[1]))\n",
    "        intlst.sort()\n",
    "        # 时间是一个环，需在列表尾加一个lst[0] + 24 * 60，实现首尾比较\n",
    "        intlst.append(intlst[0] + 24 * 60)\n",
    "\n",
    "        for i in range(len(intlst) - 1):\n",
    "            ret = min(ret, intlst[i + 1] - intlst[i])\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        intlst, ret = [], float('inf')\n",
    "        for ch in timePoints:\n",
    "            tmplst = ch.split(':')\n",
    "            intlst.append(int(tmplst[0]) * 60 + int(tmplst[1]))\n",
    "        intlst.sort()\n",
    "        intlst.append(intlst[0] + 1440)\n",
    "\n",
    "        for i in range(len(intlst) - 1):\n",
    "            ret = min(ret, intlst[i + 1] - intlst[i])\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        times = []\n",
    "        for t in timePoints:\n",
    "            h, m = map(int, t.split(':'))\n",
    "            times.append(h * 60 + m)\n",
    "        times.sort()\n",
    "        n = len(times)\n",
    "        assert n == len(timePoints)\n",
    "        ans = a = 24 * 60\n",
    "        for i, t in enumerate(times):\n",
    "            ans = min((t + a - times[(i+n-1) % n]) % a, ans)\n",
    "        return ans\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        def stamp(s):\n",
    "            return int(s[-2:]) + int(s[:2]) * 60\n",
    "\n",
    "        timePoints = sorted(map(lambda x: stamp(x), timePoints))\n",
    "        res = 24 * 60\n",
    "        for i, t in enumerate(timePoints):\n",
    "            left, right = timePoints[i - 1], timePoints[(i + 1) % len(timePoints)]\n",
    "            res = min(res, abs(t - left), abs(right - t), abs(1440 + t - left), abs(1440 + right - t))\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        a = []\n",
    "        for x in timePoints:\n",
    "            h, m = x.split(':')\n",
    "            m = int(h) * 60 + int(m)\n",
    "            a.append(m)\n",
    "        a.sort()\n",
    "        ans = inf\n",
    "        for i in range(len(a)):\n",
    "            if i == 0:\n",
    "                ans = min(ans, a[i] + 24 * 60 - a[-1])\n",
    "            else:\n",
    "                ans = min(ans, a[i] - a[i - 1])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        \"\"\"\n",
    "        问题特点：\n",
    "            1. 在此问题中，时间差 = min(|t1-t2|, |24*60+t1-t2|)\n",
    "        \"\"\"\n",
    "        # 以分钟数表示时间，并排序\n",
    "        timeMinutes = [60*int(timePoint[:2])+int(timePoint[3:]) for timePoint in timePoints]\n",
    "        timeMinutes.sort()\n",
    "\n",
    "        # 先比较 尾+24h-头\n",
    "        minDelta = 24*60+timeMinutes[0] - timeMinutes[-1]\n",
    "\n",
    "        for i in range(len(timeMinutes)-1):\n",
    "            minDelta = min(minDelta, timeMinutes[i+1]-timeMinutes[i])\n",
    "        \n",
    "        return minDelta\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        min_ls = []\n",
    "        for t in timePoints:\n",
    "            diff = int(t[:2])*60 + int(t[3:])\n",
    "            min_ls.append(diff)\n",
    "        min_ls.sort()\n",
    "        # 最大值\n",
    "        res = 2 * 10**4\n",
    "        for i in range(len(min_ls)-1):\n",
    "            if min_ls[i+1] - min_ls[i] < res:\n",
    "                res = min_ls[i+1] - min_ls[i]\n",
    "        if min_ls[0] + 24*60-min_ls[-1] < res:\n",
    "            res = min_ls[0] + 24*60-min_ls[-1]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        tmp = [self.covert(tp) for tp in timePoints]\n",
    "        tmp.sort()\n",
    "        res = float(\"inf\")\n",
    "        for i in range(1, len(tmp)):\n",
    "            res = min(res, tmp[i] - tmp[i - 1])\n",
    "        res = min(res, tmp[0] + 24*60 - tmp[-1])\n",
    "        return res \n",
    "\n",
    "    def covert(self, time_point):\n",
    "        hour, minutes = time_point.split(\":\")\n",
    "        res = int(hour) * 60 + int(minutes)\n",
    "        return res \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from bisect import bisect_left\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "\n",
    "        def timepointToInt(tp):\n",
    "            h, m = tp.split(':')\n",
    "            return int(h) * 60 + int(m) \n",
    "        \n",
    "        ps = set([timepointToInt(tp) for tp in timePoints])\n",
    "        if len(ps) != len(timePoints):\n",
    "            return 0\n",
    "        ps = sorted(list(ps))\n",
    "\n",
    "        ret = float('inf')\n",
    "\n",
    "        for i, p in enumerate(ps[:-1]):\n",
    "            ret = min(\n",
    "                ret, ps[i + 1] - ps[i]\n",
    "            )\n",
    "        \n",
    "        ret = min(ret, ps[0] + 24 * 60 - ps[-1])\n",
    "\n",
    "        return ret\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        lst = []\n",
    "        for x in sorted(timePoints):\n",
    "            t = int(x[:2]) * 60 + int(x[3:])\n",
    "            lst.append(t)\n",
    "        ans = min([y - x for x, y in pairwise(lst)])\n",
    "\n",
    "\n",
    "        return min(lst[0] + 60 * 24 - lst[-1], ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        time=[]\n",
    "        time_dif=[]\n",
    "        for x in timePoints:\n",
    "            hour=int(x[0])*10+int(x[1])\n",
    "            mins=int(x[3])*10+int(x[4])\n",
    "            time.append(hour*60+mins)\n",
    "        time.sort()\n",
    "        time.append(time[0]+1440)\n",
    "        for i in range(len(time)-1):\n",
    "            time_dif.append(time[i+1]-time[i])\n",
    "        return min(time_dif)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        timePoints = sorted(list(map(lambda x: 60*int(x[:2])+int(x[-2:]), timePoints)))\n",
    "        timePoints.append(1440+timePoints[0]) \n",
    "        timePoints = [abs(timePoints[i] - timePoints[i+1]) for i in range(len(timePoints)-1)]\n",
    "        return min(timePoints) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        def check(x):\n",
    "            first=i[0:2]\n",
    "            second=i[3:5]\n",
    "            total=int(first)*60+int(second)\n",
    "            return total \n",
    "        list1=[]\n",
    "        for i in timePoints:\n",
    "            list1.append(check(i))\n",
    "        list1.sort()\n",
    "        list1.append(list1[0]+1440)\n",
    "        \n",
    "        list2=[]\n",
    "        for i in range(1,len(list1)):\n",
    "            list2.append(list1[i]-list1[i-1])\n",
    "        \n",
    "        return min(list2)\n",
    "\n",
    "\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        def toInt(s):\n",
    "            return int(s[:2]) * 60 + int(s[3:])\n",
    "        t = sorted([toInt(i) for i in timePoints])\n",
    "        t.append(t[0] + 1440)\n",
    "        return min([v - u for u, v in pairwise(t)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        def check(tm):\n",
    "            hour,minute = tm.split(':')\n",
    "            return int(hour)*60 + int(minute)\n",
    "        \n",
    "        lst = sorted([check(time) for time in timePoints])\n",
    "        n = len(lst)\n",
    "        ans = lst[0] + 24*60 - lst[-1]\n",
    "        for i in range(n-1):\n",
    "            if lst[i+1] - lst[i] < ans:\n",
    "                ans = lst[i+1] - lst[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        time_minuses = list()\n",
    "        for time_point in timePoints:\n",
    "            time_minus = int(time_point[:2]) * 60 + int(time_point[3:])\n",
    "            time_minuses.append(time_minus)\n",
    "        time_minuses = sorted(time_minuses)\n",
    "        min_time_gap = 1440\n",
    "        for i in range(len(time_minuses)):\n",
    "            time_gap = self.timeMinus(time_minuses[i], time_minuses[(i+1) % len(time_minuses)])\n",
    "            if min_time_gap > time_gap:\n",
    "                min_time_gap = time_gap\n",
    "        return min_time_gap\n",
    "    \n",
    "    def timeMinus(self, time_f, time_r):\n",
    "        if time_r >= time_f:\n",
    "            return min(time_r - time_f, 1440 - time_r + time_f)\n",
    "        else:\n",
    "            return min(time_f - time_r, 1440 - time_f + time_r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        def convertTime(str1):\n",
    "            return (int)(str1[:2]) * 60 + (int)(str1[-2:])\n",
    "\n",
    "        array = []\n",
    "        for item in timePoints:\n",
    "            array.append(convertTime(item))\n",
    "\n",
    "        array = sorted(array)\n",
    "        min1 = 1439\n",
    "        for i in range(0,len(array)):\n",
    "            print(i)\n",
    "            print(array[i] ,array[i-1])\n",
    "            min1 = min(min1, (array[i] - array[i-1] + 1440) % 1440, (-array[i-1] + array[i] + 1440) % 1440 )\n",
    "        return min1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        def stamp(s):\n",
    "            return int(s[-2:]) + int(s[:2]) * 60\n",
    "\n",
    "        timePoints = sorted(map(lambda x: stamp(x), timePoints))\n",
    "        res = 24 * 60\n",
    "        print(timePoints)\n",
    "        for i, t in enumerate(timePoints):\n",
    "            left, right = timePoints[i - 1], timePoints[(i + 1) % len(timePoints)]\n",
    "            res = min(res, abs(t - left), abs(right - t), abs(1440 + t - left), abs(1440 + right - t))\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        def transform(time_data):\n",
    "            hour, minute = time_data.split(\":\")\n",
    "            return int(hour) * 60 + int(minute)\n",
    "\n",
    "        sort_time = sorted([transform(t) for t in timePoints])\n",
    "        res = sort_time[0] + 24 * 60 - sort_time[-1]\n",
    "        for i in range(1, len(sort_time)):\n",
    "            res = min(res, sort_time[i]-sort_time[i-1])\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\tdef findMinDifference(self, timePoints):\n",
    "\t\ttimes = [int(t[:2]) * 60 + int(t[-2:]) for t in timePoints]\n",
    "\t\ttimes.sort()\n",
    "\t\ttimes.append(times[0] + 1440)\n",
    "\t\treturn min([times[i + 1] - times[i] for i in range(len(times) - 1)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from sortedcontainers import SortedList\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "      n1 = int(''.join(timePoints[0].split(\":\")))\n",
    "      n1 = (n1 // 100) * 60 + n1 % 100\n",
    "      number = SortedList([n1])\n",
    "      result = float('inf')\n",
    "      for i in range(1, len(timePoints)):\n",
    "          temp = int(''.join(timePoints[i].split(\":\")))\n",
    "          temp = (temp // 100) * 60 + temp % 100\n",
    "          index = number.bisect_right(temp)\n",
    "          \n",
    "          if index==0 or index==len(number)-1:\n",
    "              cur_result = abs(temp - number[index])\n",
    "              result = min(result, cur_result)       \n",
    "\n",
    "          if -1<index-1:\n",
    "            cur_result=abs(temp - number[index - 1])\n",
    "            result = min(result, cur_result)\n",
    "          if index+1<len(number):\n",
    "            cur_result=abs(temp - number[index])\n",
    "            result = min(result, cur_result)\n",
    "\n",
    "          result = min(result, cur_result)\n",
    "          number.add(temp)\n",
    "\n",
    "      result = min(result, abs(1440 - number[-1] + number[0]))\n",
    "\n",
    "      return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        n = len(timePoints)\n",
    "\n",
    "        newTimeList = []\n",
    "        for time in timePoints:\n",
    "            timeMinute = 60 * int(time[:2]) + int(time[3:5])\n",
    "            newTimeList.append(timeMinute)\n",
    "\n",
    "        def quick_sort(arr,i,j):\n",
    "            if i >= j: return\n",
    "            left = i\n",
    "            right = j\n",
    "            base = arr[left]\n",
    "        \n",
    "            while i < j:\n",
    "                while i < j and base <= arr[j]: j -= 1\n",
    "                while i < j and base >= arr[i]: i += 1\n",
    "                arr[i],arr[j] = arr[j],arr[i]\n",
    "\n",
    "            arr[left],arr[i] = arr[i],arr[left]\n",
    "            quick_sort(arr,left,i-1)\n",
    "            quick_sort(arr,i+1,right)\n",
    "\n",
    "        quick_sort(newTimeList,0,n-1)\n",
    "        print(newTimeList)\n",
    "\n",
    "        result = newTimeList[-1] - newTimeList[0]\n",
    "        if result >= 720: result = 1440 - result\n",
    "\n",
    "        for index in range(n-1):\n",
    "            result = min(result,newTimeList[index+1] - newTimeList[index])\n",
    "\n",
    "        return result\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        times = []\n",
    "        for i in timePoints:\n",
    "            minute = int(i[0:2])*60 + int(i[3:5])\n",
    "            times.append(minute)\n",
    "            if minute < 720:\n",
    "                times.append(minute + 1440)\n",
    "        times.sort()\n",
    "        ans = 1440\n",
    "        for i in range(1, len(times)):\n",
    "            ans = min(ans, times[i] - times[i - 1])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        ans = []\n",
    "        for time in timePoints:\n",
    "            ans.append((int(time[0:2])*60+int(time[3:]),1440+int(time[0:2])*60+int(time[3:])))\n",
    "        ans.sort()\n",
    "        mins = float('inf')\n",
    "        for i in range(1,len(ans)):\n",
    "            if ans[i][0] - ans[i-1][0] < mins:\n",
    "                mins = ans[i][0] - ans[i-1][0]\n",
    "        mins = min(mins, ans[0][1] - ans[-1][0])\n",
    "        return mins"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        l=[]\n",
    "        for word in timePoints:\n",
    "            l.append(list(map(int,word.split(\":\"))))\n",
    "        l.sort()\n",
    "        ans= (l[0][0]-l[-1][0]+24)*60+(l[0][1]-l[-1][1])\n",
    "        def check(x,y):\n",
    "            return (y[0]-x[0])*60+(y[1]-x[1])\n",
    "        for i in range(1,len(timePoints)):\n",
    "            ans=min(ans,check(l[i-1],l[i]))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        temp=list(map(lambda x : list(map(int , x.split(\":\"))),timePoints))\n",
    "        temp.sort()\n",
    "        minr=(temp[0][0]-temp[-1][0]+24)*60+temp[0][1]-temp[-1][1]\n",
    "        for i in range(1,len(temp)):\n",
    "            minr=min(minr,(temp[i][0]-temp[i-1][0])*60+temp[i][1]-temp[i-1][1])\n",
    "        \n",
    "        return minr  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        timelist = list()\n",
    "        for timePoint in timePoints:\n",
    "            items = timePoint.split(\":\")\n",
    "            hour = items[0]\n",
    "            minute = items[1]\n",
    "\n",
    "            absoluteMin = int(hour) * 60 + int(minute)\n",
    "            timelist.append(absoluteMin)\n",
    "            if int(hour) < 12:\n",
    "                absoluteMin += 1440\n",
    "                timelist.append(absoluteMin)\n",
    "            \n",
    "        timelist.sort()\n",
    "        ans = float('inf')\n",
    "        for idx in range(len(timelist) - 1):\n",
    "            ans = min(ans, timelist[idx + 1] - timelist[idx])\n",
    "        \n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "\n",
    "        def gap(a, b):\n",
    "            if b < a:\n",
    "                a, b = b, a\n",
    "\n",
    "            array1 = a.split(\":\")\n",
    "            array2 = b.split(\":\")\n",
    "\n",
    "            hourGap = int(array2[0]) - int(array1[0])\n",
    "            mGap = 0\n",
    "            if hourGap != 0:\n",
    "                mGap = hourGap * 60\n",
    "            \n",
    "            mGap += (int(array2[1]) - int(array1[1]))\n",
    "            return min(mGap, 1440 - mGap)\n",
    "\n",
    "        \n",
    "        timePoints.sort()\n",
    "        tmp = []\n",
    "        tmp.extend(timePoints)\n",
    "        tmp.extend(timePoints)\n",
    "        print(tmp)\n",
    "        ans = float('inf')\n",
    "        for i in range(1, len(tmp)):\n",
    "            ans = min(ans, gap(tmp[i], tmp[i - 1]))\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        lst = []\n",
    "        for t in timePoints:\n",
    "            h,m = t.split(':')\n",
    "            lst.append([int(h),int(m)])\n",
    "        \n",
    "        def sort_func(x,y):\n",
    "            if x[0] < y[0]:\n",
    "                return -1\n",
    "            elif x[0] > y[0]:\n",
    "                return 1\n",
    "            else:\n",
    "                if x[1] < y[1]:\n",
    "                    return -1\n",
    "                elif x[1] > y[1]:\n",
    "                    return 1\n",
    "                else:\n",
    "                    return 0  \n",
    "\n",
    "        lst.sort(key=cmp_to_key(sort_func))\n",
    "\n",
    "        def jian(x,y):\n",
    "            return (y[0]-x[0])*60 + (y[1]-x[1])\n",
    "\n",
    "        res = (lst[-1][0]-lst[0][0])*60 + (lst[-1][1]-lst[0][1])\n",
    "        res = abs(res)\n",
    "        res = min(res, 24*60-res)\n",
    "        if len(lst) > 2:\n",
    "            for i in range(len(lst)-1):\n",
    "                temp = jian(lst[i],lst[i+1])\n",
    "                if temp < res:\n",
    "                    res = temp\n",
    "                    if temp == 0:\n",
    "                        return 0\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        ans = float('inf')\n",
    "        length = len(timePoints)\n",
    "        timePoints = [list(map(int, x.split(':'))) for x in timePoints]\n",
    "        for i in range(length):\n",
    "            for j in range(length):\n",
    "                if i == j:\n",
    "                    continue\n",
    "                a, b = timePoints[i]\n",
    "                c, d = timePoints[j]\n",
    "                delta = abs(60 * (a - c) + (b - d))\n",
    "                delta = min(delta, 1440 - delta)\n",
    "                ans = min(ans, delta)\n",
    "                if ans == 0:\n",
    "                    return 0\n",
    "        \n",
    "        return ans\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        timePoints=[time.split(':')[0]+time.split(':')[1] for time in timePoints]\n",
    "        timePoints.sort()\n",
    "\n",
    "        def fun(t1,t2):\n",
    "            h1,m1=int(t1[0:2]),int(t1[2:])\n",
    "            h2,m2=int(t2[0:2]),int(t2[2:])\n",
    "            m1+=h1*60\n",
    "            m2+=h2*60\n",
    "            return min(m2-m1,1440-(m2-m1))\n",
    "\n",
    "        res=1440\n",
    "        for ind in range(len(timePoints)-1):\n",
    "            res=min(res,fun(timePoints[ind],timePoints[ind+1]))\n",
    "        res=min(res,fun(timePoints[0],timePoints[-1]))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        mem = []\n",
    "        for i in range(len(timePoints)):\n",
    "            m = int(timePoints[i][:2])*60+int(timePoints[i][3:])\n",
    "            mem.append([i,m])\n",
    "        mem.sort(key=lambda x:x[1])\n",
    "        ans = abs(mem[0][1]+1440-mem[-1][1])\n",
    "        for i in range(1, len(mem)):\n",
    "            ans = min(ans , abs(mem[i][1]-mem[i-1][1]))\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        time_int = []\n",
    "        mini = 1441\n",
    "        for time in timePoints:\n",
    "            time_int.append(int(time.split(':')[0])*60+int(time.split(':')[1]))\n",
    "            time_int.append(int(time.split(':')[0])*60+int(time.split(':')[1])+ 1440)\n",
    "        time_int.sort()\n",
    "        \n",
    "        for i in range(len(time_int)-1):\n",
    "            mini = min(mini,time_int[i+1]-time_int[i])\n",
    "        return mini"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class DLinkNode:\n",
    "    def __init__(self, hour, minute, prev=None, next=None):\n",
    "        self.hour = hour\n",
    "        self.minute = minute\n",
    "        self.prev = prev\n",
    "        self.next = next\n",
    "\n",
    "class Solution:\n",
    "    def __init__(self):\n",
    "        self.ring_link_list = None\n",
    "        self.time_map = defaultdict()\n",
    "        for ii in range(24):\n",
    "            self.time_map[ii] = None\n",
    "        self.minDiff = 1440\n",
    "\n",
    "    def insertIn(self, node, node_before):\n",
    "        if self.ring_link_list:\n",
    "            node_next = node_before.next\n",
    "            node_before.next = node\n",
    "            node.prev = node_before\n",
    "            node.next = node_next\n",
    "            node_next.prev = node\n",
    "        else:\n",
    "            self.ring_link_list = node\n",
    "            node.next = node\n",
    "            node.prev = node\n",
    "\n",
    "    def findBefore(self, node):\n",
    "        # 环形链表中没有元素时跳出----------------\n",
    "        if not self.ring_link_list:\n",
    "            self.time_map[node.hour] = node\n",
    "            return None\n",
    "        # 对应时间表是否有元素--------------------\n",
    "        if self.time_map[node.hour]:\n",
    "            handle_node = self.time_map[node.hour]\n",
    "            if node.minute < handle_node.minute:\n",
    "                # 更新\n",
    "                self.time_map[node.hour] = node\n",
    "                return handle_node.prev\n",
    "            else:\n",
    "                last_node = None\n",
    "                while node.minute >= handle_node.minute:\n",
    "                    last_node = handle_node\n",
    "                    handle_node = handle_node.next\n",
    "                    # 回环跳出\n",
    "                    if last_node == handle_node:\n",
    "                        return last_node\n",
    "                    # 超出跳出\n",
    "                    if handle_node.hour!=node.hour:\n",
    "                        return last_node\n",
    "                return last_node\n",
    "        else:\n",
    "            hour = node.hour\n",
    "            # print(hour)\n",
    "            while not self.time_map[hour]:\n",
    "                hour -= 1\n",
    "                if hour < 0:\n",
    "                    hour += 24\n",
    "            hour_node = self.time_map[hour]\n",
    "            start_hour_node = hour_node\n",
    "            while hour_node.next.hour == hour:\n",
    "                if hour_node.next == start_hour_node:\n",
    "                    self.time_map[node.hour] = node\n",
    "                    return hour_node\n",
    "                hour_node = hour_node.next\n",
    "            self.time_map[node.hour] = node\n",
    "            return hour_node\n",
    "\n",
    "    def renewMinDiff(self, node):\n",
    "        if node:\n",
    "            next_node = node.next\n",
    "            if next_node.hour<node.hour:\n",
    "                hour_diff = next_node.hour+24-node.hour\n",
    "            else:\n",
    "                hour_diff = next_node.hour-node.hour\n",
    "            minute_diff = hour_diff * 60\n",
    "            minute_diff += next_node.minute-node.minute\n",
    "            if minute_diff<0:\n",
    "                minute_diff += 1440\n",
    "            self.minDiff = min(self.minDiff, minute_diff)\n",
    "\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        # 2<=timePoints <=2*10**4\n",
    "        minDiff = 24*60\n",
    "        for each_time in timePoints:\n",
    "            str_hour, str_minute = each_time.split(\":\")\n",
    "            hour, minute = int(str_hour), int(str_minute)\n",
    "            add_node = DLinkNode(hour, minute)\n",
    "            before_node = self.findBefore(add_node)\n",
    "            # if before_node:\n",
    "            #     print(before_node.hour, before_node.minute)\n",
    "            # else:\n",
    "            #     print(before_node)\n",
    "            self.insertIn(add_node, before_node)\n",
    "        handle_node = self.ring_link_list\n",
    "        flag = True\n",
    "        # print(\"----------------------------------------------\")\n",
    "        while flag or handle_node is not self.ring_link_list:\n",
    "            if flag:\n",
    "                flag = False\n",
    "            self.renewMinDiff(handle_node)\n",
    "            # print(handle_node.hour, handle_node.minute)\n",
    "            handle_node = handle_node.next\n",
    "            # print(self.minDiff)\n",
    "        return self.minDiff\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        \n",
    "        timePoints.sort(key = lambda x :(int(x[0:2]), int(x[3:5])))\n",
    "        print(timePoints)\n",
    "        res = 24*60\n",
    "        for i in range(len(timePoints)-1):\n",
    "            t1 = timePoints[i]\n",
    "            t2 = timePoints[i+1]\n",
    "\n",
    "            hour1 = int(t1[0:2])\n",
    "            mini1 = int(t1[3:5])\n",
    "\n",
    "            hour2 = int(t2[0:2])\n",
    "            mini2 = int(t2[3:5])\n",
    "\n",
    "            tmp_res = 60-mini1+mini2+(hour2-hour1-1)*60\n",
    "            res = min(res,tmp_res)\n",
    "\n",
    "        t1 = timePoints[-1]\n",
    "        t2 = timePoints[0]\n",
    "\n",
    "        hour1 = int(t1[0:2])\n",
    "        mini1 = int(t1[3:5])\n",
    "\n",
    "        hour2 = int(t2[0:2])\n",
    "        mini2 = int(t2[3:5])\n",
    "        tmp_res = 60-mini1+mini2 + (24-hour1-1+hour2)*60\n",
    "        res = min(res,tmp_res)\n",
    "        return res\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "\n",
    "        t = sorted(timePoints, key = lambda x:x[:2] + x[3:],reverse = True)\n",
    "\n",
    "        # return t\n",
    "        res = ((int(t[len(t) - 1][:2]) + 24) - int(t[0][:2])) * 60 +  int(t[len(t) - 1][3:]) - int(t[0][3:])\n",
    "        for i in range(len(t) - 1):\n",
    "            cur = (int(t[i][:2]) - int(t[i + 1][:2])) * 60 +  int(t[i][3:]) - int(t[i + 1][3:])\n",
    "            res = min(res,cur)\n",
    "\n",
    "        return res \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        def change2min(time):\n",
    "            hour, minute = time.split(':')\n",
    "            min1 = int(hour) * 60 + int(minute)\n",
    "            min2 = min1 + 24 * 60\n",
    "            return [min1, min2]\n",
    "\n",
    "        arr = []\n",
    "\n",
    "        for elem in timePoints:\n",
    "            arr += change2min(elem)\n",
    "\n",
    "        if len(arr) > 24 * 60:\n",
    "            return 0\n",
    "\n",
    "        arr = sorted(arr)\n",
    "\n",
    "        res = float(\"inf\")\n",
    "\n",
    "        for i in range(1, len(arr)):\n",
    "            if arr[i] - arr[i - 1] < res:\n",
    "                res = arr[i] - arr[i - 1]\n",
    "\n",
    "            if res == 0:\n",
    "                return 0\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        #转化为数字，然后找两数之差最小值\n",
    "        new_timepoints = []\n",
    "        for time in timePoints:\n",
    "            time = time.split(\":\")\n",
    "            hour,minute,total = int(time[0]),int(time[1]),0\n",
    "            if hour <= 12:\n",
    "                total = (24+hour)*60 + minute \n",
    "                new_timepoints.append(minute + hour*60)\n",
    "            else:\n",
    "                total = hour*60 + minute\n",
    "            new_timepoints.append(total)\n",
    "        new_timepoints = list(sorted(new_timepoints))\n",
    "        result = 60*24 + 60\n",
    "        print(new_timepoints)\n",
    "        for i in range(len(new_timepoints)-1):\n",
    "            min_ = new_timepoints[i+1] - new_timepoints[i]\n",
    "            if min_ == 0:\n",
    "                return 0\n",
    "            result = min(result,min_)\n",
    "        return result\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        mm = []\n",
    "        for t in timePoints:\n",
    "            tt = t.split(':')\n",
    "            v = int(tt[0]) * 60 + int(tt[1])\n",
    "            mm.append(v)\n",
    "            mm.append(v + 24 * 60)\n",
    "        mm.sort()\n",
    "        ans = inf\n",
    "        for i in range(1, len(mm)):\n",
    "            ans = min(ans, mm[i] - mm[i - 1])\n",
    "            if i == 1:\n",
    "                ans = min(ans, mm[-1] - mm[0])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMinDifference(self, timePoints: List[str]) -> int:\n",
    "        time=[]\n",
    "        time_dif=[]\n",
    "        for x in timePoints:\n",
    "            hour=int(x[0])*10+int(x[1])\n",
    "            mins=int(x[3])*10+int(x[4])\n",
    "            time.append(hour*60+mins)\n",
    "            time.append(hour*60+mins+1440)\n",
    "        time.sort()\n",
    "        for i in range(len(time)-1):\n",
    "            time_dif.append(time[i+1]-time[i])\n",
    "        return min(time_dif)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
